All Objects
Image
OBJECT: Array
new
Array
(arrayLength)
new
Array
(element0, element1, ..., elementN)
An array is an ordered set of values grouped together under a single variable name created by using an
Array
object constructor. You can create an
Array
literal by specifying the name of the array and the values of all its elements. The following example creates an array of three elements:
Code:
cars = new Array("Mercedes", "Ford", "Chrysler")
The elements of an array are indexed using their ordinal number, starting with 0. You could, therefore, refer to the second element in the above array ("Ford") as 'cars[1]'. You can specify the number of elements in a new array by using a single numeric parameter with the
Array
constructor.
For example, the following code creates an array of 7 elements:
Code:
fruit = new Array(7)
If you create an array with a single numeric parameter, that number is stored in the
length
property, and the array doesn't actually have any elements until some are specifically assigned to it. If, however, the parameter is not a number, an array of 1 element is created and that value assigned to it. You can easily increase the size of an array by assigning a value to an element higher than its current length.
NOTE:
If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use a single numeric parameter with the
Array
constructor, it will be seen as the value of a single element of the array rather than the number of elements you want that array to contain.
PROPERTIES
constructor Property
The
constructor
property contains the function that created an object's prototype.
Syntax:
object.
constructor
index Property
The read-only
index
property for an array created by a regular expression match and containing the zero-based index of that match.
Syntax:
object.
index
input Property
The read-only
input
property for an array created by a regular expression match and containing the original string against which the match was made.
Syntax:
object.
input
length Property
The
length
property holds an unsigned 32 bit integer representing the length of the array. It can be altered independently of the number of elements in the array.
Syntax:
object.
length
prototype Property
The
prototype
property allows the addition of properties to an array.
Syntax:
object.
prototype
METHODS
concat
Method
The
concat
method joins two or more
Array
objects producing one new one. The original
Array
objects are unaffected by this but, if one copy of a string or number is altered, it is not reflected in the other, whereas a change to an object reference can be seen in both copies.
Syntax:
Array.
concat
(arrayName2, arrayName3, ..., arrayNameN)
join
Method
The
join
method is used to join all the elements of an array into a single string separated by a specified string separator (if none is specified, the default is a comma).
Syntax:
Array.
join
(separator)
pop
Method
The
pop
method is used to remove and return the last element of an array. This affects the length of the array.
Syntax:
Array.
pop
()
push
Method
The
push
method is used to add one or more elements to an array, returning the new length of it. This affects the length of the array.
Syntax:
Array.
push
(element1, ..., elementN)
reverse Method
The
reverse
method, as the name implies, reverses the order of the elements in an array making the first last and the last first. Syntax:
Array.
reverse
()
shift
Method
The
shift
method removes and returns the first element of an array. This affects the length of the array.
Syntax:
Array.
shift
()
slice
Method
The
slice
method creates a new array from a selected section of an array.
Syntax:
Array.
slice
(begin[,end])
splice
Method
The
splice
method is used to add and/or remove elements of an array.
Syntax;
Array.
splice
(index, howMany, [element1][, ..., elementN])
sort
Method
The
sort
method sorts the elements of an
array
.
Syntax:
Array.
sort
(compareFunction)
toSource Method
The
toSource
method is inherited from the
Object
object and returns the source code of the array. For details see the
Object.toSource
method.
Syntax:
Array.
toSource
()
toString Method
The
toString
method is inherited from the
Object
object and returns a string representing the specified array and its elements. For more details see the
Object.toString
method.
Syntax:
Array.
toString
()
unshift
Method
The
unshift
method adds one or more elements to the beginning of an array and returns the new length.
Syntax:
Array.
unshift
(element1,..., elementN)
valueOf Method
The
valueOf
method is inherited from the
Object
object and returns a primitive value for a specified array. For details see the
Object.valueOf
method.
Syntax:
Array.
valueOf
()
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information